{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "accepted-haven",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymysql   # MySQLdb  不支持python3 用 pymysql\n",
    "import pandas as pd\n",
    "\n",
    "server='rm-bp1witqzl76lpag95.mysql.rds.aliyuncs.com'\n",
    "outserver=\"rm-bp1witqzl76lpag957o.mysql.rds.aliyuncs.com\"\n",
    "\n",
    "user=\"dbuser\"\n",
    "pwd='dbuser@121'\n",
    "dbname=\"strategy\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "important-school",
   "metadata": {},
   "outputs": [],
   "source": [
    "def execute(sql):\n",
    "    # 打开数据库连接\n",
    "    db = pymysql.connect(host=server,user=user,password=pwd,database=dbname)\n",
    "\n",
    "    # 使用 cursor() 方法创建一个游标对象 cursor\n",
    "    cursor = db.cursor()\n",
    "    \n",
    "\n",
    "    try:\n",
    "       # 执行sql语句\n",
    "       cursor.execute(sql)\n",
    "       # 提交到数据库执行\n",
    "       db.commit()\n",
    "    except Exception as ex:\n",
    "       # 如果发生错误则回滚\n",
    "        db.rollback()\n",
    "        print('error:'+ex)\n",
    "        \n",
    "    # 关闭数据库连接\n",
    "    db.close()    \n",
    "\n",
    "def select(sql):\n",
    "    # 打开数据库连接\n",
    "    db = pymysql.connect(host=server,user=user,password=pwd,database=dbname)\n",
    "\n",
    "    # 使用 cursor() 方法创建一个游标对象 cursor\n",
    "    cursor = db.cursor()\n",
    "    \n",
    "\n",
    "    try:\n",
    "         # 使用 execute()  方法执行 SQL 查询 \n",
    "        cursor.execute(sql)\n",
    "\n",
    "        # 使用 fetchone() 方法获取单条数据.\n",
    "        # data = cursor.fetchone()\n",
    "        # 使用 fetchone() 方法获取单条数据.\n",
    "        data = cursor.fetchall()\n",
    "    except Exception as ex:\n",
    "        print('error:'+ex)\n",
    "\n",
    "    # 关闭数据库连接\n",
    "    db.close()\n",
    "    \n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "nuclear-practice",
   "metadata": {},
   "outputs": [],
   "source": [
    "insert1 =\"\"\"insert stockpool(market,symbol, stockname, plateid, strategyid, memo, addtime, modifytime)\n",
    "    values('SH', '600685', '中船防务', -1, -1, '', now(), now()) \"\"\"\n",
    "\n",
    "insert4 =\"\"\"insert stockpool(market,symbol, stockname, plateid,strategyid, memo, addtime, modifytime)\n",
    "    values('SH', '6001899', '资金矿业', -1, -1,'', now(), now()) \"\"\"\n",
    "\n",
    "\n",
    "insert2 =\"\"\"insert stockpool(market,symbol, stockname, plateid,strategyid, memo, addtime, modifytime)\n",
    "    values('SZ', '00230', '科大讯飞', -1, -1,'', now(), now()) \"\"\"\n",
    "\n",
    "insert3 =\"\"\"insert stockpool(market,symbol, stockname, plateid,strategyid, memo, addtime, modifytime)\n",
    "    values('SZ', '002459', '晶澳科技', -1, -1,'', now(), now()) \"\"\"\n",
    "\n",
    "\n",
    "insert5 =\"\"\"insert stockpool(market,symbol, stockname, plateid,strategyid, memo, addtime, modifytime)\n",
    "    values('SZ', '00651', '格力电器', -1, -1,'', now(), now()) \"\"\"\n",
    "\n",
    "insert6 =\"\"\"insert stockpool(market,symbol, stockname, plateid,strategyid, memo, addtime, modifytime)\n",
    "    values('SH', '600559', '老白干酒', -1, -1,'', now(), now()) \"\"\"\n",
    "\n",
    "execute(insert1)\n",
    "execute(insert2)\n",
    "execute(insert3)\n",
    "execute(insert4)\n",
    "execute(insert5)\n",
    "execute(insert6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "immediate-sacrifice",
   "metadata": {},
   "outputs": [],
   "source": [
    "sql =\"SELECT * FROM `stockpool`\"\n",
    "\n",
    "df = select(sql)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "premium-render",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SH</td>\n",
       "      <td>600685</td>\n",
       "      <td>中船防务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SZ</td>\n",
       "      <td>00230</td>\n",
       "      <td>科大讯飞</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SZ</td>\n",
       "      <td>002459</td>\n",
       "      <td>晶澳科技</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SH</td>\n",
       "      <td>6001899</td>\n",
       "      <td>资金矿业</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SZ</td>\n",
       "      <td>00651</td>\n",
       "      <td>格力电器</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SH</td>\n",
       "      <td>600559</td>\n",
       "      <td>老白干酒</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    1        2     3\n",
       "0  SH   600685  中船防务\n",
       "1  SZ    00230  科大讯飞\n",
       "2  SZ   002459  晶澳科技\n",
       "3  SH  6001899  资金矿业\n",
       "4  SZ    00651  格力电器\n",
       "5  SH   600559  老白干酒"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "dfdest = pd.DataFrame(df).iloc[:,1:4]\n",
    "dfdest"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
